package com.yj.suncity.async;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;
import android.os.Message;

import com.yj.suncity.utis.HttpUtil;

public class ThreadUtils {

	private String pathurl;
	private OnResultListener onResultListener;

	public void startThread(String path, OnResultListener onResultListener) {
		this.pathurl = path;
		this.onResultListener = onResultListener;
		startThread();
	}
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			onResultListener.jsonResult((JSONObject)msg.obj);
		};
	};

	public interface OnResultListener {
		void jsonResult(JSONObject json);
	}

	private void startThread() {
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					String json = HttpUtil.HttpClientDoGet(pathurl);
					if (json != null) {
						JSONObject jsonObject = new JSONObject(json);
						Message message=new Message();
						message.obj=jsonObject;
						handler.sendMessage(message);
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		thread.start();
		thread = null;
	}

}
